package com.ipan.weixin.msg.general;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 上报地理位置事件
 * 
 * @author iPan
 * @version 2013-12-23
 */
public class LocationEventMsg extends EventMsg {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 746155542722733446L;

	/** 地理位置纬度 */
	protected String latitude;

	/** 地理位置经度 */
	protected String longitude;

	/** 地理位置精度 */
	protected String precision;

	public LocationEventMsg() {
		super(TransportType.RECEIVE);
	}
	
	public LocationEventMsg(Document doc) {
		this();
		fromXml(doc);
	}
	
	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getPrecision() {
		return precision;
	}

	public void setPrecision(String precision) {
		this.precision = precision;
	}
	
	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		this.latitude = root.elementTextTrim(EL_LATITUDE);
		this.longitude = root.elementTextTrim(EL_LONGITUDE);
		this.precision = root.elementTextTrim(EL_PRECISION);
	}
	
	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_EVENT).append(wrapElementEnd(EL_MSG_TYPE));
		if (nonEmpty) {
			if (StringUtils.isNotEmpty(this.latitude)) {
				buf.append(wrapElementHead(EL_LATITUDE)).append(this.latitude).append(wrapElementEnd(EL_LATITUDE));
			}
			if (StringUtils.isNotEmpty(this.longitude)) {
				buf.append(wrapElementHead(EL_LONGITUDE)).append(this.longitude).append(wrapElementEnd(EL_LONGITUDE));
			}
			if (StringUtils.isNotEmpty(this.precision)) {
				buf.append(wrapElementHead(EL_PRECISION)).append(this.precision).append(wrapElementEnd(EL_PRECISION));
			}
		} else {
			buf.append(wrapElementHead(EL_LATITUDE)).append(this.latitude).append(wrapElementEnd(EL_LATITUDE));
			buf.append(wrapElementHead(EL_LONGITUDE)).append(this.longitude).append(wrapElementEnd(EL_LONGITUDE));
			buf.append(wrapElementHead(EL_PRECISION)).append(this.precision).append(wrapElementEnd(EL_PRECISION));
		}
		return buf.append("</xml>").toString();
	}

}

/*
接收：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[LOCATION]]></Event>
<Latitude>23.137466</Latitude>
<Longitude>113.352425</Longitude>
<Precision>119.385040</Precision>
</xml>
*/
